<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/14/014
 * Time: 15:18
 */

namespace app\api\controller;


use app\admin\model\Productcategory;
use app\admin\model\Seller;
use app\common\controller\Api;

class Sellerproductcategory extends Api
{
    protected $noNeedLogin = ['*'];

    /**
     * 某个商家的所有分类
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $seller_id = $this->request->request('seller_id');
        if (!$seller_id) {
            $this->error(__('Invalid parameters'));
        }
        $seller = Seller::get($seller_id);
        if (!$seller) {
            $this->error('没有这个商家');
        }
        $model = new \app\api\model\Sellerproductcategory();
        $categoryList = $model->where('seller_id', $seller_id)->select();
        $this->success('', $categoryList);
    }

    /**
     * 商家添加分类
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function add()
    {
        $seller_id = $this->request->request('seller_id');
        $name = $this->request->request('name');
        if (!$seller_id || !$name) {
            $this->error(__('Invalid parameters'));
        }
        $seller = Seller::get($seller_id);
        if (!$seller) {
            $this->error('没有这个商家');
        }
        $sellerCategory = db('sellerproductcategory')->where('name', $name)->find();
        if ($sellerCategory) {
            $this->error('该分类名称已存在');
        }

        $model = new \app\api\model\Sellerproductcategory();
        $result = $model->validate(true)->save($_POST);
        if ($result) {
            $this->success('成功');
        }
        $this->error('失败');

    }

    /**
     * 查看总后台已有分类
     * @throws \think\exception\DbException
     */
    public function selectCategory()
    {
        $list = Productcategory::all();
        $this->success('', $list);
    }


    /**
     * 修改分类
     */
    public function edit()
    {
        $id = $this->request->request('id');
        $seller_id = $this->request->request('seller_id');
        $name = $this->request->request('name');

        if (!$id || !$seller_id || !$name) {
            $this->error(__('Invalid parameters'));
        }
        $seller = Seller::get($seller_id);
        if (!$seller) {
            $this->error('没有这个商家');
        }

        $sellerCategory = db('sellerproductcategory')->where('name',$name)->find();
        if($sellerCategory){
           $this->error('该分类名称已存在');
        }
        $model = new \app\api\model\Sellerproductcategory();
        $result = $model->allowField(true)->save($_POST,['id' => $id]);
        if ($result) {
            $this->success('成功');
        }
        $this->error('失败');
    }

    /**
     * 删除分类
     * @throws \think\exception\DbException
     */
    public function delete(){
        $id = $this->request->request('id');
        $category = \app\api\model\Sellerproductcategory::get($id);
        if(!$category){
            $this->error('没有这个分类');
        }
        $result = $category->delete();
        if ($result) {
            $this->success('成功');
        }
        $this->error('失败');
    }
}